<?php
	/**
	 * 抽奖
	 */
    class Lottery extends APIBase {
		
		public $tag = "Lottery";
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			return true;
		}

		public function logic() {
			if (!isset($this->param['vercode'])) {
				$vercode = 0;
			}else{
				$vercode = (int) $this->param['vercode'];
			}

			if (!isset($this->param['ua'])) {
				$ua = " ";
			}else{
				$ua = $this->param['ua'];
			}

			if (!isset($this->param['imsi'])) {
				$imsi = " ";
			}else{
				$imsi = $this->param['imsi'];
			}

			if (!isset($this->param['imei'])) {
				$imei = " ";
			}else{
				$imei = $this->param['imei'];
			}

			if (!isset($this->param['channel'])) {
				$channel = " ";
			}else{
				$channel = $this->param['channel'];
			}

			if (Game::$lottery_isopen == 0 ) {
				$this->returnError(300, 'activity closed');
				return;
			}

			$aa = $this->data_redis->hincrBY("hu:{$this->uid}", "lottery_count", 1);
			$money = $this->data_redis->hMGet("hu:{$this->uid}", array("money", "rmb", "coin", "lottery_count"));
			if ($money['money'] < Game::$lottery_money) {
				//$this->returnError(301, 'money is too lower');
				$this->returnData(array('get_coin' =>  0, 'money' => (int) $money['money'], 'coin' => (int) $money['coin']));
				exit(0);
				//return;
			}
			
			$lottery_count = (int) $money['lottery_count'];
			if ($lottery_count > Game::$lottery_num){
				$aa = $this->data_redis->hincrBY("hu:{$this->uid}", "lottery_count", -1);
				$this->returnError(301, '一天只能抽三次哦');
				return;
			}
			

			$index = $this->get_Lottery_award();
			$award_lottery = Game::$lottery_coin;
			$coin = $award_lottery[$index];
			$money['money'] = $this->data_redis->hincrBY("hu:{$this->uid}", "money", - Game::$lottery_money);
			$money['coin'] = $this->data_redis->hincrBY("hu:{$this->uid}", "coin", $coin);
			$now = time();
			$uid = $this->uid;
			$lottery_moneys = Game::$lottery_money;
			$insert_data = array(
				'uid' => $uid,
				'money' => $lottery_moneys,
				'coin' => $coin,
				'create_time' => $now
			); 
			$this->mysql->insert("lotteryrecord",$insert_data);
	
			$this->returnData(array('get_coin' => (int) $coin, 'money' => (int) $money['money'], 'coin' => (int) $money['coin']));
		}

		private function get_Lottery_award() {
			$configArr = Game:: $configArr ;
        	$arr = array();
        	$key = md5(serialize($configArr));
        	if (!isset($arr[$key])){
                	$max = array_sum($configArr);
                	foreach ($configArr as $k => $v){
                        	$v = $v / $max * 10000;
                        	for ($i=0; $i<$v; $i++){
                                	$arr[$key][] = $k;
                        	}
                	}
        	}
        	return $arr[$key][mt_rand(0,count($arr[$key])-1)];
		} 
	
		public function after() {
    		$this->deinitDataRedis();
				$this->deinitCacheRedis();
				$this->deinitMysql();
		}
    }
?>
